import { defineStore } from "pinia";
import { store } from "@/store";
import type { commonType } from "./types";
import { setStore, getStore } from "@/utils/store";

export const useCommonStore = defineStore({
  id: "common",
  state: (): commonType => ({
    currentNavIndex: getStore({ name: "currentNavIndex" }) || 0,
    currentRoute: ""
  }),
  actions: {
    SET_CURRENT_NAV_INDEX(currentNavIndex: number) {
      this.currentNavIndex = currentNavIndex;
      setStore({ name: "currentNavIndex", content: this.currentNavIndex });
    },
    SET_ROUTE(val: string) {
      this.currentRoute = val;
    }
  },
  getters: {
    getCurrentNavIndex(): number | undefined {
      return this.currentNavIndex;
    }
  }
});

export function useCommonStoreHook() {
  return useCommonStore(store);
}
